package com.example.android_is2;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;

import com.example.android_is2.ListenerMap.LocationListenerMap;
import com.example.android_is2.ListenerMap.LocationListenerMapPathDraw;
import com.example.android_is2.db.DBInfo;
import com.example.android_is2.pathDraw.GoogleParser;
import com.example.android_is2.pathDraw.Route;
import com.example.android_is2.pathDraw.RouteOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class SeeMapRouteActivity extends MapActivity {

  List<Overlay> overlays = null;
  private MapItemizedOverlay itemizedoverlay = null;
  private MapItemizedOverlay itemizedoverlay_my = null;
  private BridgeLocation bridge = null;
  private LocationManager locationManager = null;
  @SuppressWarnings("unused")
  private LocationProvider netProvider = null;
  private MyLocationOverlay myLocationOverlay = null;
  private LocationListenerMap myLocationListener = null;
  // -------------------------------------------
  // DB
  private DBInfo db = null;
  // ------------------------------------------------
  // Location Manager = GPS Opp Network
  private final String defaultLocationManager = LocationManager.GPS_PROVIDER;
  // ---------------------------------------------
  private MapView mapView = null;

  private MapController mapController = null;

  private SingletonParametersBridge singleton_bridge = null;

  // listView
  private ArrayList<InfoGeoPoints> listView = null;

  @SuppressWarnings("unchecked")
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seemaproute);

    db = new DBInfo(getApplicationContext());
    // ----------------------------------

    mapView = (MapView) findViewById(R.id.mapviewPath); // or you can declare it directly with the
                                                        // API key
    mapView.setBuiltInZoomControls(true);
    listView = new ArrayList<InfoGeoPoints>();

    singleton_bridge = SingletonParametersBridge.getInstance();
    // ----------------------------------------------------------
    if (singleton_bridge != null) {

      listView = (ArrayList<InfoGeoPoints>) singleton_bridge.getParameter("openListItinerario");
      String modeLocation = "driving";
      // Aggiungere GEOPOINT alla mappa
      bridge = (BridgeLocation) singleton_bridge.getParameter("bridge");
      // singleton_bridge.removeParameter("bridge");

      this.locationManager = bridge.getLocationManager();
      this.netProvider = bridge.getNetProvider();
      // -------------------------------------------------
      Route route = directions_wayspoint_open(listView, modeLocation);
      if (route != null) {

        RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
        mapView.getOverlays().add(routeOverlay);

        mapController = mapView.getController();

        if (GoogleParser.start_point_for_center != null) {
          mapController.animateTo(GoogleParser.start_point_for_center);
        }
        mapView.setBuiltInZoomControls(true);
        // Impostiamo la distanza dello Zoom a 12 (valori validi da 1 a 21).
        mapController.setZoom(7);

        // Aggiungere GEOPOINT alla mappa
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        overlays = mapView.getOverlays();

        overlays.add(myLocationOverlay);
        mapView.postInvalidate();

        Drawable drawable = this.getResources().getDrawable(R.drawable.marker_geopoint);
        itemizedoverlay = new MapItemizedOverlay(drawable, this);

        drawable = this.getResources().getDrawable(R.drawable.marker_geopoint2);
        itemizedoverlay_my = new MapItemizedOverlay(drawable, this);

        // Listener per acquisire cambiamenti Location
        myLocationListener = new LocationListenerMapPathDraw(mapView, overlays, itemizedoverlay_my,
            itemizedoverlay, db);
      }
      locationManager.requestLocationUpdates(defaultLocationManager, 10000, 10, myLocationListener);
      // --------------------------------------

    }
  }

  private Route directions_wayspoint_open(ArrayList<InfoGeoPoints> listViewItinerario, String modeLocation) {
    // TODO Auto-generated method stub
    GoogleParser parser;
    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(listViewItinerario.get(0).getLat()+","+listViewItinerario.get(0).getLong());

    sBuf.append("&destination=");

    sBuf.append(listViewItinerario.get(listViewItinerario.size() - 1).getLat()+","+listViewItinerario.get(listViewItinerario.size() - 1).getLong());
    if (listViewItinerario.size() > 2) {
      sBuf.append("&waypoints=");
      for (int i = 1; i < listViewItinerario.size() - 1; i++) {
        if (i == 1) {
          sBuf.append(listViewItinerario.get(i).getLat()+","+listViewItinerario.get(i).getLong());
        } else {
          sBuf.append("|" + listViewItinerario.get(i).getLat()+","+listViewItinerario.get(i).getLong());
        }
      }
    }
    sBuf.append("&sensor=true");
    sBuf.append("&mode=");
    sBuf.append(modeLocation);
    Log.i(MainActivity.TAG + getLocalClassName(), "URL: " + sBuf.toString());
    parser = new GoogleParser(sBuf.toString());
    Route r = parser.parse();
    if (r != null) {
      return r;
    } else {
      return null;
    }
  }

  @Override
  protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

  }

  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager != null) {
      locationManager.removeUpdates(myLocationListener);
      myLocationOverlay.disableMyLocation();
    }
    SingletonParametersBridge.getInstance().removeParameter("openListItinerario");
  }

  // private Route directions(final GeoPoint start, final GeoPoint dest) {
  @SuppressWarnings("unused")
  private Route directions(final String start, final String dest, String modeLocation) {
    GoogleParser parser;
    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start);

    sBuf.append("&destination=");

    sBuf.append(dest);
    sBuf.append("&sensor=true");
    sBuf.append("&mode=");
    sBuf.append(modeLocation);
    Log.i(MainActivity.TAG + getLocalClassName(), "URL: " + sBuf.toString());
    parser = new GoogleParser(sBuf.toString());
    Route r = parser.parse();
    if (r != null) {
      return r;
    } else {
      return null;
    }
  }

  @SuppressWarnings("unused")
  private Route directions_wayspoint(@SuppressWarnings("hiding")
  final ArrayList<String> listView, String modeLocation) {
    GoogleParser parser;
    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(listView.get(0).toLowerCase());

    sBuf.append("&destination=");

    sBuf.append(listView.get(listView.size() - 1).toLowerCase());
    if (listView.size() > 2) {
      sBuf.append("&waypoints=");
      for (int i = 1; i < listView.size() - 1; i++) {
        if (i == 1) {
          sBuf.append(listView.get(i).toLowerCase());
        } else {
          sBuf.append("|" + listView.get(i).toLowerCase());
        }
      }
    }
    sBuf.append("&sensor=true");
    sBuf.append("&mode=");
    sBuf.append(modeLocation);
    Log.i(MainActivity.TAG + getLocalClassName(), "URL: " + sBuf.toString());
    parser = new GoogleParser(sBuf.toString());
    Route r = parser.parse();
    if (r != null) {
      return r;
    } else {
      return null;
    }
  }

  @Override
  protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
  }

}

